﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses

Public Class Hotels
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack = True Then

                If Request.QueryString("country") Is Nothing And Request.QueryString("city") Is Nothing And Request.QueryString("Id") Is Nothing And LCase(Request.QueryString("cat")) Is Nothing Then
                    'Country Listing
                    getHotelList("country")
                    Session("Breadcrumb") = "&nbsp;&raquo;&nbsp;Hotels"
                Else
                    If Not Request.QueryString("country") Is Nothing Then
                        'City Listing
                        getHotelList("city")
                        Session("Breadcrumb") = "&nbsp;&raquo;&nbsp;<a href=""hotels.aspx"">Hotels</a>&nbsp;&raquo;&nbsp;" & UCase(Mid(Request.QueryString("country"), 1, 1)) & LCase(Mid(Request.QueryString("country"), 2))
                    End If
                    If Not LCase(Request.QueryString("city")) Is Nothing Then
                        If Not Request.QueryString("sort") Is Nothing Then
                            'Hotels Listing by City and Category
                            getHotels(Request.QueryString("city"), Request.QueryString("sort"))
                        Else
                            'Hotels Listing by City
                            getHotels(Request.QueryString("city"))
                        End If
                        If Not LCase(Request.QueryString("cat")) Is Nothing Then
                            'Hotels Listing by City and Category
                            'getStarHotels(Request.QueryString("cat"), Request.QueryString("city"))
                        End If
                        ltrTitle.Text = "<h2>Hotels</h2>"
                    End If
                    If Not Request.QueryString("id") Is Nothing Then
                        'Hotel Details
                        getHotel(Request.QueryString("id"))
                    End If
                End If
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Sub



    Protected Sub getHotelList(ByVal str As String)
        Dim stroutput As New StringBuilder
        Dim count As Integer = 0
        Dim i As Integer = 0
        Dim dt As DataTable
        Dim strSql As String = "select distinct " & str & " from ptn_Hotels where IsActive=1 order by " & str
        dt = Fill_Table(strSql, "Hotels")
        Select Case str
            Case LCase("country")
                stroutput.Append("<h2>Countries</h2>")
            Case LCase("city")
                stroutput.Append("<h2>Cities</h2>")
        End Select


        stroutput.Append("<div class=""countryList""><ul>")
        For Each dr As DataRow In dt.Rows
            'If count = 4 Then

            'End If
            stroutput.Append("<li><a href=""hotels.aspx?" & str & "=" & LCase(dr.Item(str).ToString) & """>" & dr.Item(str).ToString & "</a></li>")
            'count = count + 1
        Next
        stroutput.Append("</ul></div>")
        ltrHotels.Text = stroutput.ToString
    End Sub

    Protected Sub getHotels(Optional ByVal param As String = "", Optional ByVal Sort As String = "")
        Dim strSql As String = ""
        If Sort = "" Then
            'strSql = "select h.*, hct.Name as HotelCategory from Hotels h inner join HotelCategory hct on hct.Id=h.CategoryId where h.IsActive=1  and City='" & LCase(Request.QueryString("city")) & "' order by ViewOrder, Name"
            'strSql = "exec fillHotels ""and City='" & LCase(Request.QueryString("city")) & "'"""
            strSql = "SELECT h.Id, h.Name, h.CategoryId, h.Address, h.Phone, h.Email, h.Person, h.City, h.Region, h.Country, h.RoomCategory, h.RoomType, h.MealType, h.Description, h.MetaTitle, h.MetaKeywords, h.MetaDescription, h.IsActive, h.ViewOrder, h.Thumbnail, h.GMap, h.Supplements, h.CreatedOn, h.UpdatedOn, hc.Name HotelCategory FROM ptn_Hotels h Inner Join ptn_HotelCategory hc on hc.Id=h.CategoryId Where h.IsActive=1 and City='" & LCase(Request.QueryString("city")) & "' order by ViewOrder, Name"
        Else
            'strSql = "select h.*, hct.Name as HotelCategory from Hotels h inner join HotelCategory hct on hct.Id=h.CategoryId where h.IsActive=1  and City='" & LCase(Request.QueryString("city")) & "' and hct.Name='" & Request.QueryString("sort") & "' order by ViewOrder, Name"
            'strSql = "exec fillHotels ""and City='" & LCase(Request.QueryString("city")) & "' and hc.Name='" & Request.QueryString("sort") & "'"""
            strSql = "SELECT h.Id, h.Name, h.CategoryId, h.Address, h.Phone, h.Email, h.Person, h.City, h.Region, h.Country, h.RoomCategory, h.RoomType, h.MealType, h.Description, h.MetaTitle, h.MetaKeywords, h.MetaDescription, h.IsActive, h.ViewOrder, h.Thumbnail, h.GMap, h.Supplements, h.CreatedOn, h.UpdatedOn, hc.Name HotelCategory FROM ptn_Hotels h Inner Join ptn_HotelCategory hc on hc.Id=h.CategoryId Where h.IsActive=1 and City='" & LCase(Request.QueryString("city")) & "' and hct.Name='" & Request.QueryString("sort") & "' order by ViewOrder, Name"
        End If
        Fill_dtList(dlHotelsList, strSql)
    End Sub

    Protected Sub getStarHotels(ByVal cat As String, Optional ByVal city As String = "")
        Dim stroutput As New StringBuilder
        Dim strSql As String = ""
        Dim count As Integer = 0
        Dim i As Integer = 0
        Dim dt As DataTable

        'strSql = "select h.*, hct.Name as HotelCategory from Hotels h inner join HotelCategory hct on hct.Id=h.CategoryId where h.IsActive=1  and h.City='" & LCase(Request.QueryString("city")) & "' and h.CategoryId='" & LCase(Request.QueryString("cat")) & "' order by ViewOrder, Name"
        strSql = "exec fillHotels "" and City='" & LCase(Request.QueryString("city")) & "' and h.CategoryId='" & LCase(Request.QueryString("cat")) & "'"""

        dt = Fill_Table(strSql, "Hotels")

        For Each dr As DataRow In dt.Rows
            If count = 2 Then
                count = 0
                stroutput.Append("<div class=""clear""></div>")
            End If
            If i = 0 Then

                stroutput.Append("<h1>Hotels in <span>" & dr.Item("City").ToString & "</span></h1>")
            End If
            stroutput.Append("<div class=""tour"">")
            stroutput.Append("<a href=""hotels.aspx?id=" & dr.Item("Id").ToString & """ class=""tImg"">")
            If dr.Item("Thumbnail").ToString <> "" Then
                stroutput.Append("<img src=""images/" & dr.Item("Thumbnail").ToString & """ align=""left"" class=""tourimg"" />")
            Else
                stroutput.Append("<img src=""images/noimage.jpg"" align=""left"" class=""tourimg"" />")
            End If
            stroutput.Append("</a>")
            stroutput.Append("<p class=""tourTitle""><a href=""hotels.aspx?id=" & dr.Item("Id").ToString & """>" & dr.Item("Name").ToString & "</a></p>")
            stroutput.Append("<p class=""destination""><img src=""images/" & dr.Item("HotelCategory").ToString & "star.gif"" vspace=""5"" /></p>")
            stroutput.Append("<p class=""city"">" & dr.Item("City").ToString & "</p>")
            stroutput.Append("<p class=""more""><a href=""hotels.aspx?id=" & dr.Item("Id").ToString & """>View Details</a></p>")
            stroutput.Append("</div>")
            If count = 0 Then
                stroutput.Append("<div class=""space10""></div>")
            End If
            i = i + 1
        Next

        If i = 0 Then
            stroutput.Append("<p class=""red"" align=""center"">No hotels found. <br/><br/><a href=""javascript:history.go(-1)"">Go back</a></p>")
        End If

        ltrHotels.Text = stroutput.ToString

    End Sub

    Protected Function fillStars(ByVal Id As Integer)
        Dim dt As DataTable
        Dim str As New StringBuilder
        dt = Fill_Table("select * from ptn_HotelCategory where IsActive=1", "HotelCategory")
        str.Append("<select onchange=""hotelByCat(this.value,'hotels.aspx?city=" & LCase(Request.QueryString("city")) & "')"">")
        str.Append("<option value=""0"">--Select--</option>")
        For Each dr As DataRow In dt.Rows
            If Request.QueryString("sort") = dr.Item("Name").ToString Then
                str.Append("<option value=""" & dr.Item("Name").ToString & """ selected>" & dr.Item("Caption").ToString & "</option>")
            Else
                str.Append("<option value=""" & dr.Item("Name").ToString & """>" & dr.Item("Caption").ToString & "</option>")
            End If
        Next
        str.Append("</select>")
        Return str
    End Function

    Protected Sub getHotel(ByVal Id As Integer)
        Dim stroutput As New StringBuilder
        Dim strSql As String = ""
        Dim count As Integer = 0
        Dim i As Integer = 0
        Dim dt As DataTable
        dt = Fill_Table("SELECT [Id], [Name], [CategoryId], [Address], [Phone], [Email], [Person], [City], [Region], [Country], [RoomCategory], [RoomType], [MealType], [Description], [MetaTitle], [MetaKeywords], [MetaDescription], [IsActive], [ViewOrder], [Thumbnail], [GMap], [Supplements] FROM [Hotels] where IsActive=1 and Id=" & Id, "Hotels")

        'dt = Fill_Table("exec fillHotel " & Id, "Hotels")
        For Each dr As DataRow In dt.Rows
            stroutput.Append("<h1>" & dr.Item("Name").ToString & "</h1>")

            Session("Breadcrumb") = "&nbsp;&raquo;&nbsp;<a href=""hotels.aspx"">Hotels</a>&nbsp;&raquo;&nbsp;<a href=""hotels.aspx?country=" & LCase(dr.Item("Country").ToString) & """>" & dr.Item("Country").ToString & "</a>&nbsp;&raquo;&nbsp;<a href=""hotels.aspx?city=" & LCase(dr.Item("city").ToString) & """>" & SCase(dr.Item("city").ToString) & "</a>&nbsp;&raquo;&nbsp;" & dr.Item("Name").ToString

            stroutput.Append("<ul class=""tourTabs"" id=""tourTabs"">")
            stroutput.Append("<li><a rel=""Includes"">Description </a></li>")
            stroutput.Append("<li><a rel=""Gallery"">Image Gallery </a></li>")
            stroutput.Append("<li class=""tabSel""><a rel=""Cost"">Book</a></li>")
            stroutput.Append("<li><a href=""javascript:showDiv('tell')"">Tell a Friend </a></li>")

            stroutput.Append("<div id=""tell"" style=""visibility: hidden;"" class=""hideshow"">")
            stroutput.Append("<div class=""popup_block"">")
            stroutput.Append("<div class=""popup"">")
            stroutput.Append("<a href=""javascript:hideDiv('tell')"">")
            stroutput.Append("<img src=""images/close.gif"" class=""cntrl"" title=""Close""></a>")
            stroutput.Append("<iframe scrolling=""no"" src=""EmailFriend.aspx?Rel=http://www.philatourism.com" & Request.RawUrl().ToString() & "&hotelid=" & Id & """ allowtransparency=""true"" style=""width: 500px; height: 450px; margin: 0; padding: 0; overflow-y: auto;"" frameborder=""0""></iframe>")
            stroutput.Append("</div>")
            stroutput.Append("</div>")
            stroutput.Append("</div>")

            stroutput.Append("<li>")

            stroutput.Append("<a href=""http://www.addthis.com/bookmark.php?v=250&amp;username=xa-4d0c685b760572ac"" target=""_blank"" class=""addthis_button_compact"">Share</a>")


            stroutput.Append("</li>")
            stroutput.Append("</ul>")

            stroutput.Append("<div class=""tabcontent"" id=""Includes"">")
            stroutput.Append("<p>" & Server.HtmlDecode(dr.Item("Description").ToString) & "</p>")
            stroutput.Append("</div>")

            stroutput.Append("<div class=""tabcontent"" id=""Cost"">")

            Dim s1 As String = Date.Now.Day.ToString & "-" & Date.Now.Month.ToString

            stroutput.Append(getCost(Id))

            stroutput.Append("</div>")

            stroutput.Append("<div class=""tabcontent"" id=""Gallery"">")
            stroutput.Append(fillGallery(Id))
            stroutput.Append("</div>")
        Next
        ltrHotelDetails.Text = stroutput.ToString
    End Sub

    Protected Function fillGallery(ByVal Id As Integer)
        Dim str As New StringBuilder
        Dim dt As New DataTable
        Dim count As Integer = 0
        dt = Fill_Table("select g.* from ptn_Gallery g inner join ptn_HotelGallery hg on hg.GalleryId=g.Id where g.IsActive=1 and hg.HotelId=" & Id, "Gallery")
        str.Append("<table cellspacing=""3"" cellpadding=""2"" align=""center""><tr>")
        For Each dr As DataRow In dt.Rows
            If count = 4 Then
                str.Append("</tr><tr>")
                count = 0
            End If
            str.Append("<td width=""25%"" align=""center"" class=""galimg"">")
            str.Append("<a href=""images/" & dr.Item("Image").ToString & """ rel=""lightbox[" & Id & "]"" title=""" & dr.Item("Caption").ToString & """><img src=""images/" & dr.Item("Thumbnail").ToString & """ /></a>")
            str.Append("<td>")
            count = count + 1
        Next
        If count = 1 Then
            str.Append("<td width=""25%"">&nbsp;</td>")
            str.Append("<td width=""25%"">&nbsp;</td>")
            str.Append("<td width=""25%"">&nbsp;</td>")
        ElseIf count = 2 Then
            str.Append("<td width=""25%"">&nbsp;</td>")
            str.Append("<td width=""25%"">&nbsp;</td>")
        ElseIf count = 3 Then
            str.Append("<td width=""25%"">&nbsp;</td>")
        End If
        str.Append("</tr></table>")
        Return str.ToString
    End Function

    Protected Function getCost(ByVal Id As Integer)
        Try
            Dim stroutput As New StringBuilder
            Dim strSql As String = ""
            Dim count As Integer = 0
            Dim i As Integer = 0
            Dim dt As DataTable
            dt = Fill_Table("SELECT distinct rc.Id,rc.Name FROM ptn_RoomCategory rc inner join ptn_HotelCost hc on hc.RoomCategoryId=rc.Id where rc.IsActive=1 and hc.HotelId=" & Id & " and hc.Cost<>''", "RoomCategory")

            stroutput.Append("<table cellpadding=""3"" cellspacing=""0"" align=""center"" width=""100%"">")
            For Each dr As DataRow In dt.Rows

                stroutput.Append("<tr><td valign=""top"">")
                stroutput.Append("<table width=""100%"" cellspacing=""0"" cellpadding=""5"" border=""0"" class=""tablebg"">")
                If i = 0 Then
                    stroutput.Append("<tr>")
                    stroutput.Append("<td valign=""top"" width=""15%""><strong>Property Type<br />(Meal Type)</strong></td>")
                    stroutput.Append("<td valign=""top""><strong>Rates</strong></td>")
                    stroutput.Append("</tr>")
                End If
                stroutput.Append("<tr class=""trBg""><td valign=""middle"" width=""15%"" colspan=""2"">")

                stroutput.Append("<div>")

                stroutput.Append("<div class=""subhead"">" & dr.Item("Name").ToString() & "</div>")

                Dim dtMeal As DataTable
                dtMeal = Fill_Table("SELECT distinct mc.Id,mc.Name,mc.Summary,mc.IsActive FROM ptn_MealType mc inner join ptn_HotelCost hc on hc.MealType=mc.Id where mc.IsActive=1 and hc.HotelId=" & Id & " and hc.RoomCategoryId=" & dr.Item("Id").ToString, "MealType")
                For Each drMeal As DataRow In dtMeal.Rows
                    stroutput.Append("<div>")
                    stroutput.Append("<div class=""boxMeal"">")
                    stroutput.Append(drMeal.Item("Name").ToString())
                    stroutput.Append("</div>")

                    Dim dtCost As DataTable

                    Dim strClient As String
                    If Session("Client") Is Nothing Then
                        strClient = "b2c"
                    Else
                        strClient = Session("Client")
                    End If
                    dtCost = Fill_Table("select hc.*, rt.Name from ptn_hotelcost hc inner join ptn_roomtype rt on rt.id=hc.roomtypeid where hc.hotelid=" & Id & " and hc.roomcategoryid=" & dr.Item("Id").ToString & " and hc.mealtype=" & drMeal.Item("Id").ToString & " and clienttype='" & strClient & "' and (getdate() between startdate and enddate) order by startdate desc, enddate desc", "HotelCost")
                    stroutput.Append("<table cellpadding=""2"" cellspacing=""2""><tr><td width=""480px"">")
                    For Each drCost As DataRow In dtCost.Rows
                        stroutput.Append("<div class=""box80"">")
                        stroutput.Append("<div><b>" & drCost.Item("Name").ToString() & "</b></div>")
                        stroutput.Append("<div class=""price"">" & formatPrice(drCost.Item("Cost").ToString()) & "</div>")
                        stroutput.Append("</div>")
                        count = count + 1
                    Next

                    stroutput.Append("</td>")
                    stroutput.Append("<td valign=""middle""><a href=""bookhotel.aspx?id=" & Id & "&cid=" & dr.Item("Id").ToString & "&mid=" & drMeal.Item("Id").ToString & """><img src=""images/btn_booknow.jpg"" /></a></td>")
                    stroutput.Append("</tr></table>")

                    stroutput.Append("<div class=""clear""></div>")
                    stroutput.Append("</div>")
                Next


                stroutput.Append("<div class=""clear""></div>")
                stroutput.Append("</div>")

                stroutput.Append("</td>")
                stroutput.Append("</tr>")
                stroutput.Append("</table>")


                stroutput.Append("</td></tr>")

                i = i + 1
            Next
            If i = 0 Or count = 0 Then
                Dim strSub As String
                If Not Request.QueryString("Id") Is Nothing Then
                    Dim txt As Array
                    txt = RetriveData("select Name, City from ptn_Hotels where Id=" & Request.QueryString("Id"), 1)
                    strSub = txt(0) & " (" & txt(1) & ")"
                Else
                    strSub = "Hotel"
                End If
                stroutput.Remove(0, stroutput.Length)
                stroutput.Append("<div class=""subhead"">Send a Request on <a href=""mailto:enquiry@philatourism.com?Subject=Request for " & strSub & """>enquiry@philatourism.com</a></div>")
            End If
            stroutput.Append("</table>")
            Return stroutput
        Catch ex As Exception
            Return "No records found."
        End Try
    End Function

    Private Function Select_Checkboxes(ByVal ids As String, ByVal tablename As String)
        Dim strType As String = ""
        If ids IsNot Nothing Then
            If ids.ToString().Trim() <> "" Then
                Dim dt As New DataTable
                dt = Fill_Table("select * from " & tablename & " where id in (" & ids & ") order by Name", "MealType")
                For Each dr As DataRow In dt.Rows
                    strType = strType & dr.Item("Name").ToString & ", "
                Next
            End If
        End If
        Return strType
    End Function


End Class